#include "msg_pack_transfer.h"
#include <csmsg_protocol/cubedb_msg.h>

using namespace std;
using namespace ezycli;

CMsgPara* my_create_para(short msgid, short msgtype)
{
    switch(msgtype)
    {
    case Request:
        {
            switch(msgid)
            {
            case ID_S2C_REQ_UPDATE_CUBE: return new CReqUpdateCubePara;
            case ID_S2C_REQ_FETCH_CUBE: return new CReqFetchCubePara;
            default: return NULL;
            }
        }

    case Response:
        {
            switch(msgid)
            {
            case ID_S2C_REQ_UPDATE_CUBE: return new CRspUpdateCubePara;
            case ID_S2C_REQ_FETCH_CUBE: return new CRspFetchCubePara;
            default: return NULL;
            }
        }
    case Notify:
        {
            switch(msgid)
            {
            case ID_C2S_NOTIFY_CUBE: return new CNotifyCubePara;
            case ID_S2C_NOTIFY_SVR_INIT: return new CNotifySvrInitPara;
            default: return NULL;
            }
        }

    default:
        {
            return NULL;
        }
    }
}

int main(int argc, char** argv)
{
    unsigned int uin = 51000096;
    CMsg::RegisterProtocolCreator(my_create_para);
    csmsg_transfer trans;
    trans.initialize();
    trans.add_service("10.6.206.183", 80);
    int ret = trans.prepare_transfer();
    if (ret)
    {
        printf("preparation failed:%d\n", ret);
        return -1;
    }

    CReqFetchCubePara *reqparam = new CReqFetchCubePara();
    reqparam->m_shQueryNum = 1;
    TCubeQuery &query = reqparam->m_astQueryCube[0];
    memset(&query, 0, sizeof(query));
    query.m_uiUin = uin;
    query.m_shQueryRowNum = 1;
    query.m_astQueryRow[0].m_iCubeID = 1027;

    CCSHead cshead;
    cshead.m_nTotalLength = 0;
    cshead.m_nDialogID = -1;
    cshead.m_byOptLength = 0;
    cshead.m_iUin = uin;

    CMsg msg;
    msg.m_stHead.m_shMsgID = ID_S2C_REQ_FETCH_CUBE;
    msg.m_stHead.m_shMsgType = Request;
    msg.m_stHead.m_iMsgSeq = 0;
    msg.m_stHead.m_bSrcFE = 0;
    msg.m_stHead.m_bDstFE = 0;
    msg.m_stHead.m_shSrcID = 0;
    msg.m_stHead.m_shDstID = 0;
    msg.m_pMsgPara = reqparam;

    ret = trans.send_csmsg(cshead, msg);
    delete msg.m_pMsgPara;
    msg.m_pMsgPara = NULL;

    if (ret)
    {
        printf("send failed ret:%d\n", ret);
        return 0;
    }

    ret = trans.recv_csmsg(&cshead, &msg);
    if (ret)
    {
        printf("recv failed ret:%d\n", ret);
        return 0;
    }
    
    return 0;
}
